from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import csv
import time


def getWeather(base_url, location, year):
    weather_info = []
    service = Service('C:\\Users\\26407\\Desktop\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe')
    driver = webdriver.Chrome(service=service)

    try:
        for month in range(1, 13):
            # Format month to two digits
            month_str = f'{month:02d}'
            url = f'{base_url}{location}/{year}{month_str}.html'

            driver.get(url)
            wait = WebDriverWait(driver, 10)

            while True:
                weather_elements = driver.find_elements(By.XPATH, '//*[@class="thrui"]/li')

                for li in weather_elements:
                    day_weather_info = {}
                    try:
                        date_time_text = li.find_element(By.XPATH, './div[1]').text
                        high_text = li.find_element(By.XPATH, './div[2]').text
                        low_text = li.find_element(By.XPATH, './div[3]').text
                        weather_text = li.find_element(By.XPATH, './div[4]').text

                        day_weather_info['date_time'] = date_time_text.split(' ')[0]
                        day_weather_info['high'] = high_text[:high_text.find('℃')]
                        day_weather_info['low'] = low_text[:low_text.find('℃')]
                        day_weather_info['weather'] = weather_text
                        weather_info.append(day_weather_info)
                    except NoSuchElementException:
                        continue

                try:
                    load_more_button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "lishidesc2")))
                    load_more_button.click()
                    # Wait for new data to load
                    wait.until(lambda driver: len(driver.find_elements(By.XPATH, '//*[@class="thrui"]/li')) > len(
                        weather_elements))
                except TimeoutException:
                    print(f"No more data to load for {year}-{month_str}.")
                    break

    finally:
        driver.quit()

    return weather_info

def write_to_csv(weather_data, filename):
    with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['时间', '最高气温', '最低气温', '天气'])
        writer.writerows([list(day_weather_dict.values()) for day_weather_dict in weather_data])


base_url = 'https://lishi.tianqi.com/'
location = ['hefei', 'huangshan', 'wuhu', 'maanshan', 'anqing', 'huainan', 'fuyang', 'huaibei', 'tongling', 'bozhou', 'xuancheng', 'bengbu', 'chaohu', 'luan', 'chuzhou', 'chizhou', 'suzhou']  # You can change this to your desired location
year = '2024'
for location1 in location:
    csv_name = f"{location1}_{year}.csv"
    weather = getWeather(base_url, location1, year)
    write_to_csv(weather, csv_name)
    print("Data has been written to weather.csv")
    print('location1 finish')

